package core;

import gitobject.Commit;
import repository.Repository;
import fileoperation.FileStatus;
import sha1.SHA1;
import stage.Index;
import utils.setOperation;

import java.io.File;
import java.util.*;

import static core.JitReset.getAllBlob;

public class JitStatus {
    public static void getStatus() throws Exception {
        String workTree = Repository.getWorkTree();
        String gitDir = Repository.getGitDir();

        List<File> filesList = FileStatus.workTreeList(workTree);
        //workTree hash filename
        HashMap<String, String> workFiles = new HashMap<>();
        //index hash filename
        HashMap<String, String> indexFiles = Index.getIndexMap();

        for(File file:filesList) {
            workFiles.put(SHA1.getHash(file), file.toString().replace(workTree+File.separator,""));
        }

        //对比hash 工作区有 暂存区没有 说明没有add 或者被修改了
        HashSet<String> workSet = setOperation.getDifference(workFiles,indexFiles);
        //对比hash 暂存区有工作区没有，说明 工作区删除了 或者修改了
        HashSet<String> indexSet = setOperation.getDifference(indexFiles,workFiles);
        //工作区修改了 没有重新add到暂存区
        HashMap<String, String> modifiedFiles= new HashMap<>();
        HashMap<String, String> deletedFiles= new HashMap<>();
        //比较index与repo
        HashSet<String> newFiles = new HashSet<>();
        HashMap<String, String> repoFiles = new HashMap<>();
        if(Commit.getLastCommit() != null){
            Commit lastCommit =  Commit.deserialize(Commit.getLastCommit());
            repoFiles = getAllBlob(lastCommit.getTree());
        }
        newFiles = setOperation.getDifference(indexFiles,repoFiles);

        for(Iterator<String> iterator=workSet.iterator(); iterator.hasNext();){
            String hash = iterator.next();
            String fileName = workFiles.get(hash);
            //hash值不同 文件名相同
            if(indexFiles.containsValue(fileName)){
                modifiedFiles.put(hash, fileName);
                iterator.remove();
            }
        }
        for(Iterator<String> iterator=indexSet.iterator(); iterator.hasNext();){
            String hash = iterator.next();
            String fileName = indexFiles.get(hash);
            //hash值不同 文件名相同
            if(workFiles.containsValue(fileName)){
                deletedFiles.put(hash, fileName);
                iterator.remove();
            }
        }

        //read branch name
        Scanner scanner = new Scanner(new File(gitDir+File.separator+"HEAD"));
        String branchName = scanner.nextLine().substring(16).replace("\n", "");
        System.out.println("on branch "+branchName);
        if(Commit.getLastCommit() == null){
            System.out.println("No commits yet");
        }
        // 记录workTree index repo 是否一致
        boolean isEqual = true;

        if(!newFiles.isEmpty()){
            isEqual = false;
            System.out.println("Changes to be committed:");
            for(String hash: newFiles){
                System.out.println("\t"+"new file:\t"+indexFiles.get(hash));
            }
            System.out.println();
        }

        if(!modifiedFiles.isEmpty() || !deletedFiles.isEmpty()){
            isEqual = false;
            System.out.println("Changes not staged for commit:\n" +
                    "  (use \"jit add <file>...\" to update what will be committed)");
            for(String hash: modifiedFiles.keySet()){
                System.out.println("\t"+"modified:\t"+modifiedFiles.get(hash));
            }
            for(String hash :indexSet){
                System.out.println("\t"+"deleted:\t"+ indexFiles.get(hash));
            }
            System.out.println();
        }

        if(!workSet.isEmpty()){
            isEqual = false;
            System.out.println(
                    "Untracked files:\n" +
                            "  (use \"jit add <file>...\" to include in what will be committed)" );
            for(String hash: workSet){
                System.out.println("\t"+workFiles.get(hash));
            }
            // 这句应该出现在workTree != index == repo
            if(newFiles.isEmpty()){
                System.out.println("nothing added to commit but untracked files present (use \"jit add\" to track)");
            }
        }

        if(isEqual){
            if(Commit.getLastCommit() != null){
                System.out.println("nothing to commit, working tree clean");
            }else{
                System.out.println("nothing to commit (create/copy files and use \"jit add\" to track)");
            }
        }
    }

}
